package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置起始和数量
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //复制数据
        SetmealVO setmealVO= new SetmealVO();
        BeanUtils.copyProperties(setmealPageQueryDTO,setmealVO);
        //查询数据
        Page<SetmealVO> setmealPage = setmealMapper.page(setmealVO);
        //返回数据
        return new PageResult(setmealPage.getTotal(),setmealPage.getResult());
    }

    /**
     * 新增菜品
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        //复制数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //新增套餐
        setmealMapper.save(setmeal);
        //新增套餐菜品关系
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if (setmealDishList != null && setmealDishList.size() > 0){
            //循环绑定id
            setmealDishList.stream().forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.saveBatch(setmealDishList);
        }

    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO findById(Long id) {
        Setmeal setmeal = setmealMapper.seleteById(id);
        List<SetmealDish> list = setmealDishMapper.seleteBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        //复制数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //更新套餐信息
        setmealMapper.update(setmeal);
        //更新套餐菜品关系表，先删除再进行更改
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0){
            //删除套餐菜品关系
            setmealDishMapper.deleteById(setmealDTO.getId());
            //循环更新套餐id
            setmealDishes.stream().forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            setmealDishMapper.saveBatch(setmealDishes);
        }

    }

    /**
     * 删除套餐/
     *
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        Integer count = setmealMapper.findBySetmealId(ids);
        System.out.println(count);
        if (count > 0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        for (Long id : ids) {
            setmealMapper.deleteById(id);
            setmealDishMapper.deleteById(id);
        }
    }

    /**
     * 设置套装状态
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //如果是要禁用，需要判断套餐中的菜品是否在售
        if (StatusConstant.DISABLE.equals(status)){
            Integer count = setmealDishMapper.getDishStatusById(id);
            if (count > 0){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据分类id查询套餐
     *
     * @param categoryId
     * @return
     */

    @Override
    public List<Setmeal> list(Long categoryId) {
        List<Setmeal> list = setmealMapper.list(categoryId);
        return list;
    }

    /**
     * 根据套餐id查询包含的菜品
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishBySetmealId(Long setmealId) {

        List<DishItemVO> dishItemVOList = setmealDishMapper.getDishBySetmealId(setmealId);

        return dishItemVOList;
    }
}
